using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace MAF.Common
{
    /// <summary>
    /// 词语转换类，协助把词语从一种形式转换成另一种形式
    /// </summary>
	public class WordsConverter
    {
        #region 分隔符转换
        /// <summary>
        /// 把多种分隔符转换成另一种，可以把多个连续的分隔符转换成一个分隔符，已重载
        /// </summary>
        /// <param name="words">待转换的词语</param>
        /// <param name="oldSeparator"></param>
        /// <param name="newSeparator"></param>
        /// <param name="options">是否把连续的分隔符转换成一个分隔符</param>
        /// <returns>转换后的词语，输入词语如果是null将会返回空字符串</returns>
        public static string ToAnotherSeparator(string words, string[] oldSeparator,string newSeparator, StringSplitOptions options)
        {
            if (string.IsNullOrEmpty(words)) return "";
            return string.Join( newSeparator, words.Split(oldSeparator, options) );
        }
        /// <summary>
        /// 重载
        /// </summary>
        public static string ToAnotherSeparator(string words, char[] oldSeparator,string newSeparator, StringSplitOptions options)
        {
            if (string.IsNullOrEmpty(words)) return "";
            return string.Join( newSeparator, words.Split(oldSeparator, options) );
        }
        #endregion

        #region 大小写转换
        /// <summary>
        /// 把词语第一个字母变成大写
        /// </summary>
        /// <param name="words"></param>
        /// <returns></returns>
        public static string ToUpperA(string words,string separator)
        {
            if (string.IsNullOrEmpty(words)) return "";
            string[] ss = words.Split(new string[]{separator},StringSplitOptions.None);
            for( int i=0,len=ss.Length; i<len; ++i )
            {
                ss[i] = WordConverter.ToUpperA(ss[i]);
            }
            return string.Join(separator, ss);
        }

        /// <summary>
        /// 把词语第一个字母变成小写
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToLowerA(string words, string separator)
        {
            if (string.IsNullOrEmpty(words)) return "";
            string[] ss = words.Split(new string[]{ separator },StringSplitOptions.None);
            for (int i = 0, len = ss.Length; i < len; ++i)
            {
                ss[i] = WordConverter.ToLowerA(ss[i]);
            }
            return string.Join(separator, ss);
        }

        #endregion

        #region 转换为PascalCase，CamelCase
        /// <summary>
        /// PascalCase 帕斯卡命名，又称UpperCamelCase,一般用户命名空间，类，属性，方法的命名
        /// 本方法会自动去除词语中的非单词字符和下划线
        /// 解析：在大多数的正则表达式实现中，“单词字符”通常包括<<[a-zA-Z0-9_]>>
        /// </summary>
        /// <param name="words"></param>
        /// <returns></returns>
        public static string ToPascalCase(string words)
        {
            if( string.IsNullOrEmpty(words) ) return "";

            string[] ss = Regex.Split(words, @"\W|_");

            if (1 == ss.Length)
                return WordConverter.ToUpperA(ss[0]);

            for( int i=0,len=ss.Length;i<len;i++ )
            {
                ss[i] = ss[i].ToLower();
                ss[i] = WordConverter.ToUpperA(ss[i]);
            }
            return string.Join( "",ss );
        }
        
        /// <summary>
        /// CamelCase 骆驼命名,一般用于变量命名
        /// </summary>
        /// <param name="words"></param>
        /// <returns></returns>
        public static string ToCamelCase(string words)
		{
            return WordConverter.ToLowerA( ToPascalCase(words) );
        }
        #endregion
    }
}